package com.xxx.car.web.api;

import com.xxx.car.entity.vo.request.*;
import com.xxx.car.entity.vo.response.RespUserCar;
import com.xxx.car.service.UserCarService;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by xuwenjie on 2017/6/14.
 */
@Controller
@RequestMapping("/api/car")
public class ApiUserCarController {

    @Autowired
    private UserCarService userCarService;

    private final static Logger LOG = LoggerFactory.getLogger(ApiUserCarController.class);

    @RequestMapping("/list")
    @ResponseBody
    public APIResponse list(@Valid @ModelAttribute ReqUserCarQuery query, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        List<RespUserCar> list = userCarService.query(query);
        return APIResponse.returnSuccess(list);
    }

    @RequestMapping("/get")
    @ResponseBody
    public APIResponse get(@Valid @ModelAttribute ReqUserCarGet reqUserCarGet, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        RespUserCar car;
        if (reqUserCarGet.getCarId() != null) {
            car = userCarService.getByIdAndUser(reqUserCarGet.getCarId(), reqUserCarGet.getUserId());
        } else {
            car = userCarService.getUserDefaultCar(reqUserCarGet.getUserId());
        }
        return APIResponse.returnSuccess(car);
    }

    @RequestMapping("/setDefault")
    @ResponseBody
    public APIResponse setDefault(@Valid @ModelAttribute ReqUserCarSetDefault reqUserCarSetDefault, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        userCarService.setDefaultCar(reqUserCarSetDefault.getUserId(), reqUserCarSetDefault.getCarId());
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/del")
    @ResponseBody
    public APIResponse del(@Valid @ModelAttribute ReqUserCarDel reqUserCarDel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        userCarService.deleteByIdAndUser(reqUserCarDel.getCarId(), reqUserCarDel.getUserId());
        return APIResponse.returnSuccess();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public APIResponse saveOrUpdate(@Valid @ModelAttribute ReqUserCar reqUserCar, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        userCarService.saveOrUpdate(reqUserCar);
        return APIResponse.returnSuccess();
    }

}
